//
//  B_GoodsDetailVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/9/22.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_GoodsDetailVC.h"
#import "B_SubmitFooterView.h"
#import "B_GoodsDetailHeaderView.h"
#import "B_TopicDetailHeaderTwo.h"
#import "B_AddGoodsLabelTVC.h"
#import "B_AddGoodsImageViewTVC.h"
#import "B_GoodsDetailModel.h"


@interface B_GoodsDetailVC ()

@property (nonatomic, strong) BaseView * mainView;

@property (nonatomic, strong) B_SubmitFooterView * submitFooter;

@property (nonatomic, strong) B_GoodsDetailModel * detailModel;

@end

@implementation B_GoodsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setRightImageBarButtonItemWithFrame:CGRectZero image:@"" selectImage:@"" ishidden:YES action:^(GUIButton *button) {
        // TODO:执行分享
    }];
    self.view = self.mainView;
    [self.mainView addSubview:self.submitFooter];
    CGFloat bottomHeight = getTabHeight();
    [self.submitFooter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mainView.mas_left);
        make.right.equalTo(self.mainView.mas_right);
        make.bottom.equalTo(self.mainView.mas_bottom);
        make.height.equalTo(@(bottomHeight));
    }];
    [_mainView setTableViewFrameWithTop:-getNavHeight() left:0 right:0 bottom:-bottomHeight];
    [self getData];
}

- (BaseView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseView alloc] init];
        
        
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_AddGoodsLabelTVC" bundle:nil] forCellReuseIdentifier:@"B_AddGoodsLabelTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_AddGoodsImageViewTVC" bundle:nil] forCellReuseIdentifier:@"B_AddGoodsImageViewTVC"];
        
        
        /// TODO:设置表有多少组.
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 2;
        };
        //TODO:设置每组有多少的cell
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 0;
            } else {
                return weSelf.detailModel.goodsInfoListArray.count;
            }
            return 1;
        };
        
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_GoodsDetailHeaderView * view = [B_GoodsDetailHeaderView initB_GoodsDetailHeaderView];
                view.array = weSelf.detailModel.goodsPictureListArray;
                view.B_goodsTitle.text = weSelf.detailModel.goodsName;
                view.B_goodsPrice.text = weSelf.detailModel.goodsPrice;
                view.B_goodsContent.text = weSelf.detailModel.goodsDes;
                view.B_goodsSale.text = [NSString stringWithFormat:@"月销%@笔", safeValue1(weSelf.detailModel.goodsSalesVolume)];
                view.B_goodsType.text = @"包邮";
                
                
                return view;
            } else {
                B_TopicDetailHeaderTwo * header = [B_TopicDetailHeaderTwo initB_TopicDetailHeaderTwo];
                header.B_button.hidden = YES;
                header.B_titleLabel.text = @"商品介绍";
                return header;
            }
            return nil;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 1) {
                //TODO:需要判断模型  是 图片还是  文本  从而加载  图片或者文本.
                B_GoodsInfoList * model = weSelf.detailModel.goodsInfoListArray[indexPath.row];
                if (ISNULL(model.content)) {
                    B_AddGoodsImageViewTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_AddGoodsImageViewTVC" forIndexPath:indexPath];
                    [cell.B_imageView sd_setImageWithURL:[NSURL URLWithString:model.picture]];
                    return cell;

                } else {
                    
                    B_AddGoodsLabelTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_AddGoodsLabelTVC" forIndexPath:indexPath];
                    cell.B_label.text = model.content;
                    return cell;
                    
                }
            }
            UITableViewCell * cell = [[UITableViewCell alloc] init];
            return cell;
        };
        
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            return nil;
        };
        
        
    }
    return _mainView;
}

- (B_SubmitFooterView *)submitFooter {
    if (!_submitFooter) {
        _submitFooter = [B_SubmitFooterView initB_SubmitFooterView];
        [_submitFooter.B_SubmitButton setTitle:@"立即购买" forState:UIControlStateNormal];
        _submitFooter.B_SubmitButton.backgroundColor = [UIColor color_pink];
        _submitFooter.submitAction = ^{
            // TODO:执行购买的方法
        };
    }
    return _submitFooter;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavigationBarBackgroundWithImage:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
    [self setNavigationBarShadowImge:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
}



- (void) getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/goods/selectGoodsInfo/%@", self.B_GoodsId] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            weSelf.detailModel = [B_GoodsDetailModel mj_objectWithKeyValues:data[@"values"]];
            weSelf.detailModel.goodsPictureListArray = [NSMutableArray array];
            weSelf.detailModel.goodsInfoListArray = [NSMutableArray array];
            for (NSDictionary * dic  in weSelf.detailModel.goodsPictureList) {
                [weSelf.detailModel.goodsPictureListArray addObject:dic[@"goodsPictures"]];
            }
            for (NSDictionary * dic in weSelf.detailModel.goodsInfoList) {
                B_GoodsInfoList * model = [B_GoodsInfoList mj_objectWithKeyValues:dic];
                [weSelf.detailModel.goodsInfoListArray addObject:model];
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
